

const ajax = (obj)=>{
    
    const {
        //数据请求类型
        type,
        //服务器端地址
        path,
        //本地发送请求携带发送的数据
        data = {},
        //服务器端返回数据类型
        dataType = 'json',
        //ajax请求时同步还是异步, 默认为异步
        async = true,
        //接受服务器端发送的数据后执行的操作
        success
    } = obj;

    //对携带发送的数据进行格式化
    let params = '';
    for (const key in data) {
        params += `${key}=${data[key]}&`;
    }
    params = params.substring(0, params.length - 1);

    // 发送请求
    const xhr = new XMLHttpRequest();

    //判断请求方式来执行对应的代码
    if(type === 'get'){
        xhr.open(type, path + '?' + params, async);
        xhr.send();
    }else if(type === 'post'){
        xhr.open(type, path, async);
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
        xhr.send(params);
    }
    //等待接受服务端发送数据
    xhr.addEventListener('readystatechange', ()=>{
        if(xhr.readyState === 4 && xhr.status === 200){
            let res = xhr.responseText;
            if(dataType.toLowerCase() === 'json'){
                res = JSON.parse(res);
            }
            success(res);
        }
    });

}


// 切割地址栏 拿到指定的字段名
const queryString = (name)=>{
    const url = location.search.replace('?', '');

    let arr = url.split('&');
    
    arr = arr.map(v=>{
        return {
            name : v.split('=')[0],
            content : v.split('=')[1]
        }
    });
    console.log(arr);
    const res = arr.filter((v) => {
        
        return v.name === name;
    })[0];
    
    if(res){
        return res.content;
    }
    return '';
}


//封装一个promise
const request = (type, path, data, dataType, async)=>{
    return new Promise((resolve, reject) => {
        ajax({
            type,
            path,
            data,
            dataType,
            async,
            success : (res) => {
                resolve(res);
            }
        });
    });
}


//添加标签页
const add_pages = (length, obj)=>{
    let html = '';
    for(let i = length; i >= 1; i--){
        html += `
            <button>${i}</button>
        `;
    }
    return html;

}

//商品页面生成
const goods_des = (list)=>{
    html = '';
    list.forEach((v, e) => {
        // console.log(v);
        html += `
            <li>
                <a href="../html/detail.html?cloths_id=${v.cloths_id}">
                    <div>
                        <i class="iconfont icon-aixin"></i>
                    </div>
                    <img
                    src= ${v.cloths_img}
                    alt=""
                    />
                    <h4>${v.cloths_name}</h4>
                    <p>￥${v.cloths_prices}</p>
                    <a>立即购买
                        <i class="iconfont icon-youjiantou"></i>
                    </a>
                </a>
          </li>
        `;
    });

    return html;
}


// 注册页面
const register = (data)=>{
    return request('post', '../php/register.php', data);
}

//登录页面
const login = (data) =>{
    return request('post', '../php/login.php', data);
}

//商品展示
const goods = (data) =>{
    return request('get', '../php/all_goods.php',data);
}

//轮播图
const carouselFigure = (data) =>{
    return request('get', '../php/main/carouselFigure.php');
}

// 渲染生成分类数据
const human_sort = (data)=>{
    return request('get', '../php/human.php', data);
}

// 渲染生成女士服装列表
const females_cloths_sort = (data)=>{
    return request('get', '../php/female_cloths.php',data);
}

// 渲染详情页
const cloths_details = (data)=>{
    return request('get', '../php/deatils.php', data);
}

//添加购物车
const add = (data)=>{
    return request('get', '../php/add_goods.php', data);
}

// 渲染购物车页面
const cart = (data)=>{
    return request('get', '../php/cart.php', data);
}

// 购物车的操作

// 购物车单删
const del_one = (data)=>{
    return request('get', '../php/cart_oper/delete_one.php', data);
}

// 选中删除

const del_checked = (data)=>{
    return request('get', '../php/cart_oper/delete_checked.php', data);
}

// 加号添加
const goods_adds = (data)=>{
    return request('get', '../php/cart_oper/add.php', data);
}
//减号减少
const goods_subs = (data)=>{
    return request('get', '../php/cart_oper/sub.php', data);
}

//修改
const goods_change = (data)=>{
    return request('get', '../php/cart_oper/change.php', data);
}

